﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Skill
	/// </summary>
	public partial class Skill
	{
		public Skill()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string name)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Skill");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Maticsoft.Model.Skill model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Skill(");
			strSql.Append("name,desc,mp,damage,damage_hot,damage_hot_time,damage_poison,curse_at,hp_suck,mp_suck,curse_de,needPoint,kind)");
			strSql.Append(" values (");
			strSql.Append("@name,@desc,@mp,@damage,@damage_hot,@damage_hot_time,@damage_poison,@curse_at,@hp_suck,@mp_suck,@curse_de,@needPoint,@kind)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String),
					new SQLiteParameter("@desc", DbType.String),
					new SQLiteParameter("@mp", DbType.Int32,4),
					new SQLiteParameter("@damage", DbType.Int32,4),
					new SQLiteParameter("@damage_hot", DbType.Int32,4),
					new SQLiteParameter("@damage_hot_time", DbType.Int32,4),
					new SQLiteParameter("@damage_poison", DbType.Int32,4),
					new SQLiteParameter("@curse_at", DbType.Int32,4),
					new SQLiteParameter("@hp_suck", DbType.Int32,4),
					new SQLiteParameter("@mp_suck", DbType.Int32,4),
					new SQLiteParameter("@curse_de", DbType.Int32,4),
					new SQLiteParameter("@needPoint", DbType.Int32,4),
					new SQLiteParameter("@kind", DbType.String)};
			parameters[0].Value = model.name;
			parameters[1].Value = model.desc;
			parameters[2].Value = model.mp;
			parameters[3].Value = model.damage;
			parameters[4].Value = model.damage_hot;
			parameters[5].Value = model.damage_hot_time;
			parameters[6].Value = model.damage_poison;
			parameters[7].Value = model.curse_at;
			parameters[8].Value = model.hp_suck;
			parameters[9].Value = model.mp_suck;
			parameters[10].Value = model.curse_de;
			parameters[11].Value = model.needPoint;
			parameters[12].Value = model.kind;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Skill model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Skill set ");
			strSql.Append("desc=@desc,");
			strSql.Append("mp=@mp,");
			strSql.Append("damage=@damage,");
			strSql.Append("damage_hot=@damage_hot,");
			strSql.Append("damage_hot_time=@damage_hot_time,");
			strSql.Append("damage_poison=@damage_poison,");
			strSql.Append("curse_at=@curse_at,");
			strSql.Append("hp_suck=@hp_suck,");
			strSql.Append("mp_suck=@mp_suck,");
			strSql.Append("curse_de=@curse_de,");
			strSql.Append("needPoint=@needPoint,");
			strSql.Append("kind=@kind");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@desc", DbType.String),
					new SQLiteParameter("@mp", DbType.Int32,4),
					new SQLiteParameter("@damage", DbType.Int32,4),
					new SQLiteParameter("@damage_hot", DbType.Int32,4),
					new SQLiteParameter("@damage_hot_time", DbType.Int32,4),
					new SQLiteParameter("@damage_poison", DbType.Int32,4),
					new SQLiteParameter("@curse_at", DbType.Int32,4),
					new SQLiteParameter("@hp_suck", DbType.Int32,4),
					new SQLiteParameter("@mp_suck", DbType.Int32,4),
					new SQLiteParameter("@curse_de", DbType.Int32,4),
					new SQLiteParameter("@needPoint", DbType.Int32,4),
					new SQLiteParameter("@kind", DbType.String),
					new SQLiteParameter("@name", DbType.String)};
			parameters[0].Value = model.desc;
			parameters[1].Value = model.mp;
			parameters[2].Value = model.damage;
			parameters[3].Value = model.damage_hot;
			parameters[4].Value = model.damage_hot_time;
			parameters[5].Value = model.damage_poison;
			parameters[6].Value = model.curse_at;
			parameters[7].Value = model.hp_suck;
			parameters[8].Value = model.mp_suck;
			parameters[9].Value = model.curse_de;
			parameters[10].Value = model.needPoint;
			parameters[11].Value = model.kind;
			parameters[12].Value = model.name;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Skill ");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string namelist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Skill ");
			strSql.Append(" where name in ("+namelist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Skill GetModel(string name)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select name,desc,mp,damage,damage_hot,damage_hot_time,damage_poison,curse_at,hp_suck,mp_suck,curse_de,needPoint,kind from Skill ");
			strSql.Append(" where name=@name ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@name", DbType.String)			};
			parameters[0].Value = name;

			Maticsoft.Model.Skill model=new Maticsoft.Model.Skill();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Skill DataRowToModel(DataRow row)
		{
			Maticsoft.Model.Skill model=new Maticsoft.Model.Skill();
			if (row != null)
			{
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["desc"]!=null)
				{
					model.desc=row["desc"].ToString();
				}
				if(row["mp"]!=null && row["mp"].ToString()!="")
				{
					model.mp=int.Parse(row["mp"].ToString());
				}
				if(row["damage"]!=null && row["damage"].ToString()!="")
				{
					model.damage=int.Parse(row["damage"].ToString());
				}
				if(row["damage_hot"]!=null && row["damage_hot"].ToString()!="")
				{
					model.damage_hot=int.Parse(row["damage_hot"].ToString());
				}
				if(row["damage_hot_time"]!=null && row["damage_hot_time"].ToString()!="")
				{
					model.damage_hot_time=int.Parse(row["damage_hot_time"].ToString());
				}
				if(row["damage_poison"]!=null && row["damage_poison"].ToString()!="")
				{
					model.damage_poison=int.Parse(row["damage_poison"].ToString());
				}
				if(row["curse_at"]!=null && row["curse_at"].ToString()!="")
				{
					model.curse_at=int.Parse(row["curse_at"].ToString());
				}
				if(row["hp_suck"]!=null && row["hp_suck"].ToString()!="")
				{
					model.hp_suck=int.Parse(row["hp_suck"].ToString());
				}
				if(row["mp_suck"]!=null && row["mp_suck"].ToString()!="")
				{
					model.mp_suck=int.Parse(row["mp_suck"].ToString());
				}
				if(row["curse_de"]!=null && row["curse_de"].ToString()!="")
				{
					model.curse_de=int.Parse(row["curse_de"].ToString());
				}
				if(row["needPoint"]!=null && row["needPoint"].ToString()!="")
				{
					model.needPoint=int.Parse(row["needPoint"].ToString());
				}
				if(row["kind"]!=null)
				{
					model.kind=row["kind"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select name,desc,mp,damage,damage_hot,damage_hot_time,damage_poison,curse_at,hp_suck,mp_suck,curse_de,needPoint,kind ");
			strSql.Append(" FROM Skill ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Skill ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.name desc");
			}
			strSql.Append(")AS Row, T.*  from Skill T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "Skill";
			parameters[1].Value = "name";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

